﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using ServerManager.Data;
using ServerManager.Models;
using ServerManager.Repository;

namespace ServerManager.Controllers
{
    [Authorize]
    [Route("[controller]")]
    public class IpAddressController : Controller
    {
        private readonly IIpAddressRepository _ipAddressRepository;
        private readonly IRepository<Server> _serverRepository;

        public IpAddressController(IIpAddressRepository ipAddressRepository,
            IRepository<Server> serverRepository)
        {
            _ipAddressRepository = ipAddressRepository;
            _serverRepository = serverRepository;
        }

        public async Task<IActionResult> Index()
        {
            var list = await _ipAddressRepository.GetListAsync();
            return View(list);
        }


        [HttpGet("[action]/{id?}")]
        public async Task<IActionResult> CreateOrUpdate(int? id)
        {
            if (id == null)
            {
                BuildStatusList(1);
                await BuildServerList();
                return View();
            }
            else
            {
                var s = _ipAddressRepository.Find(x => x.Id == id);
                BuildStatusList(s.Status);
                await BuildServerList(s.Id);
                return View(s);
            }
        }

        [HttpPost("[action]/{id?}")]
        public async Task<IActionResult> CreateOrUpdate(int? id, IpAddress i)
        {
            if (ModelState.IsValid)
            {
                if (id == null)
                {
                    await _ipAddressRepository.AddAsync(i);
                }
                else
                {
                    if (id == i.Id)
                    {
                        await _ipAddressRepository.UpdateAsync(i);
                    }
                }
            }
            return RedirectToAction(nameof(Index));
        }

        [HttpGet("[action]/{id}")]
        public async Task<IActionResult> Delete(int id)
        {
            await _ipAddressRepository.DeleteAsync(x => x.Id == id);
            return RedirectToAction(nameof(Index));
        }

        [HttpGet("[action]")]
        public async Task<IActionResult> ImportIpAddresses()
        {
            List<IpAddress> list = new List<IpAddress>();
            for (int i = 66; i <= 126; i++)
            {
                IpAddress ip = new IpAddress
                {
                    Ip = $"222.135.141.{i}",
                    Status = 1
                };
                list.Add(ip);
            }
            for (int i = 130; i <= 158; i++)
            {
                IpAddress ip = new IpAddress
                {
                    Ip = $"222.135.143.{i}",
                    Status = 1
                };
                list.Add(ip);
            }
            await _ipAddressRepository.AddAsync(list);

            return RedirectToAction(nameof(Index));
        }



        #region help

        private void BuildStatusList(int selected)
        {
            bool s0 = false;
            bool s1 = false;
            bool s2 = false;

            if (selected == 0)
                s0 = true;
            else if (selected == 1)
                s1 = true;
            else if (selected == 2)
                s2 = true;

            var list = new List<SelectListItem>
            {
                 new SelectListItem { Text = "不可用", Value = "0", Selected = s0 },
                 new SelectListItem { Text = "空闲", Value = "1", Selected = s1 },
                 new SelectListItem { Text = "占用", Value = "2", Selected = s2 }
            };

            ViewBag.StatusList = list;
        }

        private async Task BuildServerList(object selected = null)
        {
            var list = await _serverRepository.TableNoTracking.ToListAsync();

            ViewBag.ServerList = new SelectList(list, "Id", "Position", selected);
        }
        #endregion
    }
}